package com.example.front.main.community;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.sign.User;
import com.example.front.utils.MyAdapter;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 房间类
 */
public class RoomActivity extends AppCompatActivity {
    private String URL;
    private MyApplication myApplication;
    private String name;
    private String type;
    private double price;
    private int count;
    private String remark;
    private LinearLayout name_linearLayout;
    private LinearLayout type_linearLayout;
    private LinearLayout count_linearLayout;
    private LinearLayout remark_linearLayout;
    private LinearLayout user_linearLayout;
    private ListView user_listView;
    private Button leaveButton;
    private TextView name_textView;
    private TextView price_textView;
    private TextView type_textView;
    private TextView count_textView;
    private TextView remark_textView;
    private ListView room_listView;
    private MyAdapter<Room> roomMyAdapter = null;
    private MyAdapter<User> userMyAdapter = null;
    private List<Room> roomList = null;
    private List<User> userList = null;
    private ImageView userImage;
    private int flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);
        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        name_linearLayout = findViewById(R.id.name_linearLayout);
        type_linearLayout = findViewById(R.id.type_linearLayout);
        count_linearLayout = findViewById(R.id.count_linearLayout);
        remark_linearLayout = findViewById(R.id.remark_linearLayout);
        user_linearLayout = findViewById(R.id.user_linearLayout);
        user_listView = findViewById(R.id.user_listView);
        leaveButton = findViewById(R.id.leave_button);
        name_textView = findViewById(R.id.name_textView);
        price_textView = findViewById(R.id.price_textView);
        type_textView = findViewById(R.id.type_textView);
        count_textView = findViewById(R.id.count_textView);
        remark_textView = findViewById(R.id.remark_textView);
        room_listView = findViewById(R.id.room_listView);
        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        OkHttpUtils
                .post()
                .url(URL + "/room/atRoom")
                .addParams("account", account)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Toast.makeText(RoomActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onResponse(String response) {
                        String json = response;
                        System.out.println(json);
                        Log.i("success", "获取成功！");
                        if(json.equals("")) {
                            room_listView = findViewById(R.id.room_listView);
                            room_listView.setVisibility(View.VISIBLE);
                            OkHttpUtils.get()
                                    .url(URL + "/room/getRoomList")
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            Toast.makeText(RoomActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            String json = response;
                                            Log.i("success", "获取成功！");
                                            roomList = new ArrayList<Room>();
                                            try {
                                                JSONArray jsonArray = new JSONArray(json);
                                                for (int i = 0; i < jsonArray.length(); i++){
                                                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                                    Room room = new Room();
                                                    room.setId(jsonObject.getInt("id"));
                                                    room.setName(jsonObject.getString("name"));
                                                    room.setCost(jsonObject.getDouble("cost"));
                                                    room.setType(jsonObject.getString("type"));
                                                    room.setCount(jsonObject.getInt("count"));
                                                    room.setMaxCount(jsonObject.getInt("maxCount"));
                                                    room.setState(jsonObject.getInt("state"));
                                                    room.setRemark(jsonObject.getString("remark"));
                                                    roomList.add(room);
                                                }
                                            }catch (Exception e){
                                                e.printStackTrace();
                                            }
                                            roomMyAdapter = new MyAdapter<Room>((ArrayList) roomList, R.layout.room_list_item){
                                                @Override
                                                public void bindView(ViewHolder holder, Room obj){
                                                    holder.setText(R.id.id_textView, obj.getId() + "");
                                                    holder.setText(R.id.name_textView, obj.getName() + "房（当前人数" + obj.getCount() + "人）");
                                                    holder.setText(R.id.type_textView, obj.getType() + "");
                                                    if(obj.getCount() < obj.getMaxCount() && obj.getState() == 1)holder.setText(R.id.state_textView, "可以加入");
                                                    else holder.setText(R.id.state_textView, "不可加入");
                                                }
                                            };
                                            room_listView.setAdapter(roomMyAdapter);
                                            room_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                @Override
                                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                    Room room = roomList.get(position);
                                                    if(room.getCount() == 0){
                                                        Intent intent = new Intent(RoomActivity.this, RoomCreateActivity.class);
                                                        Bundle bundle = new Bundle();
                                                        bundle.putString("name", room.getName());
                                                        bundle.putInt("maxCount", room.getMaxCount());
                                                        bundle.putDouble("cost", room.getCost());
                                                        bundle.putString("type", room.getType());
                                                        intent.putExtras(bundle);
                                                        startActivity(intent);
                                                    }
                                                    else {
                                                        Intent intent = new Intent(RoomActivity.this, RoomIntroductionActivity.class);
                                                        Bundle bundle = new Bundle();
                                                        bundle.putString("name", room.getName());
                                                        bundle.putDouble("cost", room.getCost());
                                                        bundle.putString("type", room.getType());
                                                        bundle.putInt("count", room.getCount());
                                                        bundle.putInt("maxCount", room.getMaxCount());
                                                        bundle.putInt("state", room.getState());
                                                        bundle.putString("remark", room.getRemark());
                                                        intent.putExtras(bundle);
                                                        startActivity(intent);
                                                    }
                                                }
                                            });
                                        }
                                    });
                        }
                        else {
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                name = jsonObject.getString("name");
                                price = jsonObject.getDouble("cost");
                                type = jsonObject.getString("type");
                                count = jsonObject.getInt("count");
                                remark = jsonObject.getString("remark");
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            OkHttpUtils.get()
                                    .url(URL + "/room/getRoomUserList")
                                    .addParams("name", name)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            Toast.makeText(RoomActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            int count = 0;
                                            String json = response;
                                            Log.i("success", "获取成功！");
                                            userList = new ArrayList<User>();
                                            try {
                                                JSONArray jsonArray = new JSONArray(json);
                                                for (int i = 0; i < jsonArray.length(); i++){
                                                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                                    User user = new User();
                                                    user.setAccount(jsonObject.getString("account"));
                                                    user.setUsername(jsonObject.getString("username"));
                                                    user.setPhone(jsonObject.getString("phone"));
                                                    user.setSex(jsonObject.getInt("sex"));
                                                    user.setFlag(jsonObject.getInt("flag"));
                                                    if(jsonObject.getString("photo").equals("")||jsonObject.getString("photo").equals("null"))
                                                    {
                                                        user.setPhoto("https://cloud.662359.cn/view.php/77851657c364e7e176fdd7816e311fa9.png");
                                                    }
                                                    else {
                                                        user.setPhoto(jsonObject.getString("photo"));
                                                    }
                                                    if(user.getAccount().equals(account))flag=user.getFlag();
                                                    userList.add(user);
                                                    count++;
                                                }
                                            }catch (Exception e){
                                                e.printStackTrace();
                                            }
                                            userMyAdapter = new MyAdapter<User>((ArrayList) userList, R.layout.user_list_item){
                                                @Override
                                                public void bindView(ViewHolder holder, User obj){
                                                    userImage = holder.getView(R.id.user_image);
                                                    Glide.with(RoomActivity.this)
                                                            .load(obj.getPhoto())
                                                            .error(R.drawable.error)
                                                            .crossFade()
                                                            .into(userImage);
                                                    if(obj.getAccount().equals(account))holder.setText(R.id.name_textView, obj.getUsername() + "(我)");
                                                    else holder.setText(R.id.name_textView, obj.getUsername() + "");
                                                    if(obj.getSex() == 1)holder.setText(R.id.introduction_textView, "男 " + obj.getPhone());
                                                    else holder.setText(R.id.introduction_textView, "女 " + obj.getPhone());
                                                    if(obj.getFlag() == 1)holder.setTextColor(R.id.state_textView, "等待中");
                                                    else holder.setTextColor(R.id.state_textView, "游戏中");
                                                }
                                            };
                                            room_listView.setVisibility(View.GONE);
                                            name_linearLayout.setVisibility(View.VISIBLE);
                                            type_linearLayout.setVisibility(View.VISIBLE);
                                            count_linearLayout.setVisibility(View.VISIBLE);
                                            remark_linearLayout.setVisibility(View.VISIBLE);
                                            user_linearLayout.setVisibility(View.VISIBLE);
                                            user_listView.setVisibility(View.VISIBLE);
                                            if (flag == 2)
                                            {
                                                leaveButton.setVisibility(View.GONE);
                                            }else {
                                                leaveButton.setVisibility(View.VISIBLE);
                                            }
                                            name_textView.setText(name + "房");
                                            price_textView.setText(price + "元/时");
                                            type_textView.setText(type + "");
                                            count_textView.setText(count + "人");
                                            if(remark.equals(""))remark_textView.setText("  暂无介绍");
                                            else remark_textView.setText("  " + remark);
                                            user_listView.setAdapter(userMyAdapter);
                                        }
                                    });
                        }
                    }
                });
    }

    @Override
    protected void onRestart(){
        super.onRestart();

        SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        OkHttpUtils
                .post()
                .url(URL + "/room/atRoom")
                .addParams("account", account)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Toast.makeText(RoomActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onResponse(String response) {
                        String json = response;
                        System.out.println(json);
                        if(json.equals("")) {
                            room_listView.setVisibility(View.VISIBLE);
                            OkHttpUtils.get()
                                    .url(URL + "/room/getRoomList")
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            Toast.makeText(RoomActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            String json = response;
                                            Log.i("success", "获取成功！");
                                            roomList = new ArrayList<Room>();
                                            try {
                                                JSONArray jsonArray = new JSONArray(json);
                                                for (int i = 0; i < jsonArray.length(); i++){
                                                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                                    Room room = new Room();
                                                    room.setId(jsonObject.getInt("id"));
                                                    room.setName(jsonObject.getString("name"));
                                                    room.setCost(jsonObject.getDouble("cost"));
                                                    room.setType(jsonObject.getString("type"));
                                                    room.setCount(jsonObject.getInt("count"));
                                                    room.setMaxCount(jsonObject.getInt("maxCount"));
                                                    room.setState(jsonObject.getInt("state"));
                                                    room.setRemark(jsonObject.getString("remark"));
                                                    roomList.add(room);
                                                }
                                            }catch (Exception e){
                                                e.printStackTrace();
                                            }
                                            roomMyAdapter = new MyAdapter<Room>((ArrayList) roomList, R.layout.room_list_item){
                                                @Override
                                                public void bindView(ViewHolder holder, Room obj){
                                                    holder.setText(R.id.id_textView, obj.getId() + "");
                                                    holder.setText(R.id.name_textView, obj.getName() + "房（当前人数" + obj.getCount() + "人）");
                                                    holder.setText(R.id.type_textView,obj.getType() + "");
                                                    if(obj.getCount() < obj.getMaxCount() && obj.getState() == 1)holder.setText(R.id.state_textView, "可以加入");
                                                    else holder.setText(R.id.state_textView, "不可加入");
                                                }
                                            };
                                            room_listView.setAdapter(roomMyAdapter);
                                            room_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                @Override
                                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                    Room room = roomList.get(position);
                                                    if(room.getCount() == 0){
                                                        Intent intent = new Intent(RoomActivity.this, RoomCreateActivity.class);
                                                        Bundle bundle = new Bundle();
                                                        bundle.putString("name", room.getName());
                                                        bundle.putInt("maxCount", room.getMaxCount());
                                                        bundle.putDouble("cost", room.getCost());
                                                        bundle.putString("type", room.getType());
                                                        intent.putExtras(bundle);
                                                        startActivity(intent);
                                                    }
                                                    else {
                                                        Intent intent = new Intent(RoomActivity.this, RoomIntroductionActivity.class);
                                                        Bundle bundle = new Bundle();
                                                        bundle.putString("name", room.getName());
                                                        bundle.putDouble("cost", room.getCost());
                                                        bundle.putString("type", room.getType());
                                                        bundle.putInt("count", room.getCount());
                                                        bundle.putInt("maxCount", room.getMaxCount());
                                                        bundle.putInt("state", room.getState());
                                                        bundle.putString("remark", room.getRemark());
                                                        intent.putExtras(bundle);
                                                        startActivity(intent);
                                                    }
                                                }
                                            });
                                        }
                                    });
                        }
                        else {
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                name = jsonObject.getString("name");
                                price = jsonObject.getDouble("cost");
                                type = jsonObject.getString("type");
                                count = jsonObject.getInt("count");
                                remark = jsonObject.getString("remark");
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            OkHttpUtils.get()
                                    .url(URL + "/room/getRoomUserList")
                                    .addParams("name", name)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            Toast.makeText(RoomActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            int count = 0;
                                            String json = response;
                                            Log.i("success", "获取成功！");
                                            userList = new ArrayList<User>();
                                            try {
                                                JSONArray jsonArray = new JSONArray(json);
                                                for (int i = 0; i < jsonArray.length(); i++){
                                                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                                    User user = new User();
                                                    user.setAccount(jsonObject.getString("account"));
                                                    user.setUsername(jsonObject.getString("username"));
                                                    user.setPhone(jsonObject.getString("phone"));
                                                    user.setSex(jsonObject.getInt("sex"));
                                                    user.setFlag(jsonObject.getInt("flag"));
                                                    if(jsonObject.getString("photo").equals("") || jsonObject.getString("photo").equals("null"))
                                                    {
                                                        user.setPhoto("https://cloud.662359.cn/view.php/77851657c364e7e176fdd7816e311fa9.png");
                                                    }
                                                    else {
                                                        user.setPhoto(jsonObject.getString("photo"));
                                                    }
                                                    if(user.getAccount().equals(account))flag = user.getFlag();
                                                    userList.add(user);
                                                    count++;
                                                }
                                            }catch (Exception e){
                                                e.printStackTrace();
                                            }
                                            userMyAdapter = new MyAdapter<User>((ArrayList) userList, R.layout.user_list_item){
                                                @Override
                                                public void bindView(ViewHolder holder, User obj){
                                                    userImage = holder.getView(R.id.user_image);
                                                    Glide.with(RoomActivity.this)
                                                            .load(obj.getPhoto())
                                                            .placeholder(R.drawable.loading)
                                                            .error(R.drawable.error)
                                                            .crossFade()
                                                            .into(userImage);
                                                    if(obj.getAccount().equals(account)){
                                                        holder.setText(R.id.name_textView, obj.getUsername() + "(我)");
                                                        flag = obj.getFlag();
                                                    }
                                                    else holder.setText(R.id.name_textView, obj.getUsername() + "");
                                                    if(obj.getSex() == 1)holder.setText(R.id.introduction_textView, "男 " + obj.getPhone());
                                                    else holder.setText(R.id.introduction_textView, "女 " + obj.getPhone());
                                                    if(obj.getFlag() == 1)holder.setTextColor(R.id.state_textView, "等待中");
                                                    else holder.setTextColor(R.id.state_textView, "游戏中");
                                                }
                                            };
                                            room_listView.setVisibility(View.GONE);
                                            name_linearLayout.setVisibility(View.VISIBLE);
                                            type_linearLayout.setVisibility(View.VISIBLE);
                                            count_linearLayout.setVisibility(View.VISIBLE);
                                            remark_linearLayout.setVisibility(View.VISIBLE);
                                            user_linearLayout.setVisibility(View.VISIBLE);
                                            user_listView.setVisibility(View.VISIBLE);
                                            if (flag == 2) {
                                                leaveButton.setVisibility(View.GONE);
                                            }else {
                                                leaveButton.setVisibility(View.VISIBLE);
                                            }
                                            name_textView.setText(name + "房");
                                            price_textView.setText(price + "元/时");
                                            type_textView.setText(type + "");
                                            count_textView.setText(count + "人");
                                            if(remark.equals(""))remark_textView.setText("  暂无介绍");
                                            else remark_textView.setText("  " + remark);
                                            user_listView.setAdapter(userMyAdapter);
                                        }
                                    });
                        }
                    }
                });
    }

    public void leave_room_onClick(View view) {
        SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        OkHttpUtils
                .post()
                .url(URL + "/room/leaveRoom")
                .addParams("account", account)
                .addParams("name", name)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                    }
                    @Override
                    public void onResponse(String response) {
                        if(response.equals("true")) {
                            Toast.makeText(RoomActivity.this, "退出成功！", Toast.LENGTH_SHORT).show();
                            name_linearLayout.setVisibility(View.GONE);
                            type_linearLayout.setVisibility(View.GONE);
                            count_linearLayout.setVisibility(View.GONE);
                            remark_linearLayout.setVisibility(View.GONE);
                            user_linearLayout.setVisibility(View.GONE);
                            leaveButton.setVisibility(View.GONE);
                            room_listView.setVisibility(View.VISIBLE);
                            OkHttpUtils.get()
                                    .url(URL + "/room/getRoomList")
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            Toast.makeText(RoomActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            String json = response;
                                            Log.i("success", "获取成功！");
                                            roomList = new ArrayList<Room>();
                                            try {
                                                JSONArray jsonArray = new JSONArray(json);
                                                for (int i = 0; i < jsonArray.length(); i++){
                                                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                                    Room room = new Room();
                                                    room.setId(jsonObject.getInt("id"));
                                                    room.setName(jsonObject.getString("name"));
                                                    room.setCost(jsonObject.getDouble("cost"));
                                                    room.setType(jsonObject.getString("type"));
                                                    room.setCount(jsonObject.getInt("count"));
                                                    room.setMaxCount(jsonObject.getInt("maxCount"));
                                                    room.setState(jsonObject.getInt("state"));
                                                    room.setRemark(jsonObject.getString("remark"));
                                                    roomList.add(room);
                                                }
                                            }catch (Exception e){
                                                e.printStackTrace();
                                            }
                                            roomMyAdapter = new MyAdapter<Room>((ArrayList) roomList, R.layout.room_list_item){
                                                @Override
                                                public void bindView(ViewHolder holder, Room obj){
                                                    holder.setText(R.id.id_textView, obj.getId() + "");
                                                    holder.setText(R.id.name_textView, obj.getName() + "房（当前人数" + obj.getCount() + "人）");
                                                    holder.setText(R.id.type_textView,obj.getType() + "");
                                                    if(obj.getCount() < obj.getMaxCount() && obj.getState() == 1)holder.setText(R.id.state_textView, "可以加入");
                                                    else holder.setText(R.id.state_textView, "不可加入");
                                                }
                                            };
                                            room_listView.setAdapter(roomMyAdapter);
                                            room_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                @Override
                                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                    Room room = roomList.get(position);
                                                    if(room.getCount() == 0){
                                                        Intent intent = new Intent(RoomActivity.this, RoomCreateActivity.class);
                                                        Bundle bundle = new Bundle();
                                                        bundle.putString("name", room.getName());
                                                        bundle.putInt("maxCount", room.getMaxCount());
                                                        bundle.putDouble("cost", room.getCost());
                                                        bundle.putString("type", room.getType());
                                                        intent.putExtras(bundle);
                                                        startActivity(intent);
                                                    }
                                                    else {
                                                        Intent intent = new Intent(RoomActivity.this, RoomIntroductionActivity.class);
                                                        Bundle bundle = new Bundle();
                                                        bundle.putString("name", room.getName());
                                                        bundle.putDouble("cost", room.getCost());
                                                        bundle.putString("type", room.getType());
                                                        bundle.putInt("count", room.getCount());
                                                        bundle.putInt("maxCount", room.getMaxCount());
                                                        bundle.putInt("state", room.getState());
                                                        bundle.putString("remark", room.getRemark());
                                                        intent.putExtras(bundle);
                                                        startActivity(intent);
                                                    }
                                                }
                                            });
                                        }
                                    });
                        }
                        else
                            Toast.makeText(RoomActivity.this, "退出失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    private Bitmap getBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}
